package com.pll.service.Impl;

import com.pll.constant.ConstantField;
import com.pll.controller.admin.commodity.redislogic.GoodsRedisRepository;
import com.pll.controller.admin.commodity.vo.CommodityPaginationVO;
import com.pll.controller.admin.commodity.vo.ProductInformationVO;
import com.pll.dto.ResponseDto;
import com.pll.pojo.Goods;
import com.pll.pojo.GoodsType;
import com.pll.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @className: CommodityServiceImpl
 * @author: XR_Z
 * @date: 2025/6/7 20:59
 * @Version: 1.0
 * @description: 商品ServiceImpl
 */

@Service
public class CommodityServiceImpl implements CommodityService {

    private ResponseDto dto;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private GoodsRedisRepository goodsRedisRepository;

    @Override
    public ResponseDto getItAll() {
        dto = ResponseDto.fail(0,"没有更多的商品信息了~😑");
        // 获取所有商品ID
        Set<Object> goodIds = redisTemplate.opsForSet().members(ConstantField.MerchantsKey.ALL_MERCHANT_ID);

        if (!(goodIds == null || goodIds.isEmpty())) {
            // 生成所有键
            List<String> keys = goodIds.stream()
                    .map(id -> ConstantField.MerchantsKey.ALL_MERCHANDISE + id)
                    .collect(Collectors.toList());

            // 批量获取值
            dto = ResponseDto.success(redisTemplate.opsForValue().multiGet(keys));
        }

        return dto;
    }

    @Override
    public void saveGoods(Goods goods) {
        goodsRedisRepository.saveGoods(goods);
    }

    @Override
    public void saveGoodsType(GoodsType goodsType) {
        goodsRedisRepository.saveGoodsType(goodsType);
    }

    @Override
    public ResponseDto queryGoods(CommodityPaginationVO vo) {
        dto = ResponseDto.err(-1,"没有更多商品~😑");
        HashMap<String, Object> goods = goodsRedisRepository.queryGoods(vo);

        if (!(goods == null || goods.isEmpty())) {
            dto = ResponseDto.success(goods);
        }

        return dto;
    }

    /**
     * 从redis中删除商品
     * @param id 要删除的id
     * @return dto
     */
    @Override
    public ResponseDto deleteTheItem(int id) {
        goodsRedisRepository.deleteGoodsById(id);
        dto = ResponseDto.success(1);
        return dto;
    }

    /**
     *  从redis中批量删除商品
     * @param ids 要删除的id集合
     * @return dto
     */
    @Override
    public ResponseDto deleteInBulk(List<Integer> ids) {
        goodsRedisRepository.batchDeleteGoods(ids);
        dto = ResponseDto.success(ids.size());
        return dto;
    }

    /**
     * 根据vo修改对应商品信息
     * @param productVO 要修改的商品信息
     * @return dto
     */
    @Override
    public ResponseDto modifyTheProduct(ProductInformationVO productVO) {
        goodsRedisRepository.updateGoodsByVO(productVO);
        dto = ResponseDto.success(1);
        return dto;
    }
}
